import 'package:cloud_medicine/components/util.dart';
import 'package:cloud_medicine/http/config.dart';
import 'package:cloud_medicine/http/shop_api.dart';
import 'package:cloud_medicine/model/shop_list.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class ShopEditAndAdd extends StatefulWidget {
  final TextEditingController addressController;
  final TextEditingController nameController;
  final TextEditingController phoneController;
  final TextEditingController deliController;
  final TextEditingController iniDeliphoneController;
  final TextEditingController descriController;
  final Function saveInfoAndCommit;
  final String oldaddress;
  final String oldname;
  final String oldphone;
  final String olddelivery;
  final String oldinidelivery;
  final String olddescribe;
  final num shopID;

  ShopEditAndAdd(
      {Key key,
      @required this.addressController,
      @required this.nameController,
      @required this.phoneController,
      @required this.oldaddress,
      @required this.oldname,
      @required this.oldphone,
      @required this.saveInfoAndCommit,
      @required this.deliController,
      @required this.iniDeliphoneController,
      @required this.descriController,
      @required this.olddelivery,
      @required this.oldinidelivery,
      @required this.olddescribe,
      this.shopID})
      : super(key: key);

  @override
  _ShopEditAndAddState createState() => _ShopEditAndAddState();
}

class _ShopEditAndAddState extends State<ShopEditAndAdd> {
  @override
  void initState() {
    super.initState();
    widget.addressController.text = widget.oldaddress;
    widget.nameController.text = widget.oldname;
    widget.phoneController.text = widget.oldphone;
    widget.deliController.text = widget.olddelivery;
    widget.iniDeliphoneController.text = widget.oldinidelivery;
    widget.descriController.text = widget.olddescribe;
  }

  Future _editShopInfo() async {
    await editShopInfo_api('$backIP/shopManage/editShopInfo', {
      'id': widget.shopID,
      'name': widget.nameController.text,
      'picture': imgUp,
      'see': widget.descriController.text,
      'phone': int.parse(widget.phoneController.text),
      'ini_delivery_fee': double.parse(widget.iniDeliphoneController.text),
      'delivery_fee': double.parse(widget.deliController.text),
      'address': widget.addressController.text
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          margin: EdgeInsets.all(10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              TextField(
                controller: widget.nameController,
                decoration: InputDecoration(labelText: '药店名'),
              ),
              TextField(
                controller: widget.addressController,
                decoration: InputDecoration(labelText: '地址'),
              ),
              TextField(
                inputFormatters: [
                  FilteringTextInputFormatter(RegExp("[0-9]"), allow: true)
                ],
                controller: widget.phoneController,
                decoration: InputDecoration(labelText: '电话'),
              ),
              TextField(
                inputFormatters: [
                  FilteringTextInputFormatter(RegExp("[0.0-9.0]"), allow: true)
                ],
                controller: widget.iniDeliphoneController,
                decoration: InputDecoration(
                    labelText: '起送费', hintText: '若无需起送费，可输入“0”'),
              ),
              TextField(
                inputFormatters: [
                  FilteringTextInputFormatter(RegExp("[0.0-9.0]"), allow: true)
                ],
                controller: widget.deliController,
                decoration: InputDecoration(
                    labelText: '配送费(元)', hintText: '若无需配送费，可输入“0”'),
              ),
              TextField(
                controller: widget.descriController,
                decoration: InputDecoration(labelText: '描述'),
              ),
              Container(
                margin: EdgeInsets.only(left: 20, right: 20, top: 30),
                width: double.infinity,
                height: 40,
                decoration: BoxDecoration(
                    gradient: LinearGradient(
                        colors: [Colors.lightBlueAccent, Colors.blueAccent]),
                    borderRadius: BorderRadius.circular(20)),
                child: RaisedButton(
                  child: Text(
                    '保存',
                    style: TextStyle(
                        color: Colors.white, fontWeight: FontWeight.w100),
                  ),
                  color: Colors.transparent,
                  elevation: 2,
                  highlightElevation: 0,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20)),
                  onPressed: () {
                    if (widget.addressController.text == '' ||
                        widget.nameController.text == '' ||
                        widget.phoneController.text == '' ||
                        widget.deliController.text == '' ||
                        widget.iniDeliphoneController.text == '' ||
                        widget.descriController.text == '' ||
                        imgUp.isEmpty) {
                      showDialog(
                        context: context,
                        builder: (context) => SimpleDialog(
                          title: Text('提示'),
                          children: [
                            Container(
                              margin: EdgeInsets.only(left: 10),
                              child: Text('请将内容填写完整!'),
                            ),
                            Align(
                              alignment: Alignment.centerRight,
                              child: FlatButton(
                                onPressed: () {
                                  Navigator.pop(context);
                                },
                                child: Text(
                                  '取消',
                                  style: TextStyle(color: Colors.blue),
                                ),
                              ),
                            )
                          ],
                        ),
                      );
                    } else {
                      _editShopInfo();
                      openAlertDialog(
                          context, widget.saveInfoAndCommit, '保存成功');
                    }
                  },
                ),
              )
            ],
          ),
        )
      ],
    );
  }
}
